package com.fancy.learncenter.ui.activity.base;

import android.app.Dialog;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.common.MyApplication;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.ProgressUtil;

import butterknife.ButterKnife;


/**
 * Created by Hyy on 2016/9/18.
 */
public class BaseActivity extends AppCompatActivity {
    Dialog dialog = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        MyApplication.getIntance().addActivity(this);
        // PushAgent.getInstance(this).onAppStart();
    }

    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
//        ButterKnife.bind(this);
    }

    public void setContentView(int layoutResID, Status Type) {
        this.setContentView(layoutResID);
        setType(Type);
    }

    public void setContentView(int layoutResID, Status Type, String Title) {
        this.setContentView(layoutResID);
        setType(Type);
        initToolbar(Title);
    }

    public void setCartoonContentView(int layoutResID, Status Type) {
        this.setContentView(layoutResID);
        setType(Type);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            LogUtil.MyLog("setCartoonContentView", "====setCartoonContentView==");
            ImageView back = (ImageView) findViewById(R.id.back);

            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        } else {
            LogUtil.MyLog("setCartoonContentView", "====null==");
        }
    }

    public void initToolbar(String Title) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            ImageView back = (ImageView) findViewById(R.id.back);
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    finish();
                }
            });

            TextView title = (TextView) findViewById(R.id.title);
            if (title != null) {
                title.setText(Title);
            }
        }
    }

    public void initToolbar(String Title, int color) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
            toolbar.setBackgroundColor(color);
            ImageView back = (ImageView) findViewById(R.id.back);
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });

            TextView title = (TextView) findViewById(R.id.title);
            title.setText(Title);
        }
    }

    public void setRight(String title, View.OnClickListener onClickListener) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            TextView right = (TextView) findViewById(R.id.right);
            right.setText(title);
            right.setVisibility(View.VISIBLE);
            if (onClickListener != null) {
                right.setOnClickListener(onClickListener);
            }
        }
    }

    public void setRightImg(int res, View.OnClickListener onClickListener) {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            ImageView right = (ImageView) findViewById(R.id.right_img);
            right.setImageResource(res);
            right.setVisibility(View.VISIBLE);
            if (onClickListener != null) {
                right.setOnClickListener(onClickListener);
            }
        }
    }

    public void setRightImgHide() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            ImageView right = (ImageView) findViewById(R.id.right_img);
            right.setVisibility(View.GONE);

        }
    }
    public void setRightImgShow() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            ImageView right = (ImageView) findViewById(R.id.right_img);
            right.setVisibility(View.VISIBLE);

        }
    }

    public void setType(Status type) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            switch (type) {
                case NAVIGATION:
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    break;
                case STATUS:
                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                    break;
            }
        }
    }

    public enum Status {
        NAVIGATION, STATUS, Default
    }

    public void dialogShow() {
        if (!this.isFinishing()) {
            if (dialog == null) {
                dialog = ProgressUtil.creatRequestDialog(this, "正在发送电波");
            }
            dialog.show();
        }
    }

    public void dialogDismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }


}
